package com.zzvcom.webapp.action.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.persist.model.admin.Subscription;
import com.zzvcom.service.admin.SubscriptionManager;
import com.zzvcom.template.ResultPage;
import com.zzvcom.utils.DateUtil;
import com.zzvcom.utils.ExtMessage;

@Controller
@Scope("prototype")
public class SubscriptionAction
{

	private Subscription sub;

	private SubscriptionManager manager;

	private ExtMessage msg = new ExtMessage();// 返回信息

	private Map rs = new HashMap();// 结果集合

	private ResultPage resultPage;

	private Integer start;

	private Integer limit;

	public String execute()
	{
		if (resultPage == null)
		{
			resultPage = new ResultPage();
		}
		// System.out.println("start limit"+start+":"+limit);
		resultPage.startLimitSetting(start, limit);
		DetachedCriteria dc = DetachedCriteria.forClass(Subscription.class);
		dc.addOrder(Order.desc("addtime"));
		resultPage.setDetachedCriteria(dc);
		manager.getResultPageByDetachedCriteria(resultPage);
		rs.put("root", resultPage.getResult());
		rs.put("records", resultPage.getTotalRecord());
		return "rs";
	}

	public String save()
	{
		if (StringUtils.isBlank(sub.getId()))
		{
			sub.setId(null);
			sub.setAddtime(DateUtil.dateToNum14(new Date()));
		}
		manager.save(sub);
		msg.setInfo("保存成功");
		return "msg";
	}

	public String delete()
	{
		sub = manager.get(sub.getId());
		manager.delete(sub);
		msg.setInfo("删除成功");
		return "msg";
	}

    public Subscription getSub()
    {
        return sub;
    }

    public void setSub(Subscription sub)
    {
        this.sub = sub;
    }

    public SubscriptionManager getManager()
    {
        return manager;
    }
    @Autowired
    public void setManager(SubscriptionManager manager)
    {
        this.manager = manager;
    }

    public ExtMessage getMsg()
    {
        return msg;
    }

    public void setMsg(ExtMessage msg)
    {
        this.msg = msg;
    }

    public Map getRs()
    {
        return rs;
    }

    public void setRs(Map rs)
    {
        this.rs = rs;
    }

    public ResultPage getResultPage()
    {
        return resultPage;
    }

    public void setResultPage(ResultPage resultPage)
    {
        this.resultPage = resultPage;
    }

    public Integer getStart()
    {
        return start;
    }

    public void setStart(Integer start)
    {
        this.start = start;
    }

    public Integer getLimit()
    {
        return limit;
    }

    public void setLimit(Integer limit)
    {
        this.limit = limit;
    }



}
